package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.goods.GoodsSpecListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsSpecSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsSpecServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/goodsSpec")
public class GoodsSpecCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    GoodsSpecServ goodsSpecServ;

    @GetMapping("")
    public Result findListPage(String name, Long goodsId,PaginationParam pager) {
        GoodsSpecListDTO dto = goodsSpecServ.findListPage(name,goodsId,pager);
        return Result.success(dto);
    }

    @GetMapping("/{id}")
    public Result findDetail(@PathVariable Long id) {
        return Result.success(goodsSpecServ.findDetail(id));
    }
    @PostMapping
    public Result save(@Validated @RequestBody GoodsSpecSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品规格详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(goodsSpecServ.save(param));
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody GoodsSpecSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存商品详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(goodsSpecServ.update(id,param));
    }
    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(goodsSpecServ.deleted(id));
    }



    //商品上下架
    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id,AvailableStatus status){
        return Result.success(goodsSpecServ.changeStatus(id,status));
    }
}
